package com.studyweb.controller;

import com.studyweb.pojo.entity.Note;
import com.studyweb.service.NoteService;
import com.studyweb.pojo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/note")
@CrossOrigin
public class NoteController {
    
    @Autowired
    private NoteService noteService;
    
    /**
     * 添加笔记
     */
    @PostMapping
    public Result<String> addNote(@RequestBody Note note) {
        try {
            boolean result = noteService.addNote(note);
            if (result) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (Exception e) {
            return Result.error("添加失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除笔记
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteNote(@PathVariable Long id) {
        try {
            boolean result = noteService.deleteNote(id);
            if (result) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量删除笔记
     */
    @DeleteMapping("/batch")
    public Result<String> deleteNotes(@RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return Result.error("请选择要删除的笔记");
            }
            
            boolean result = noteService.deleteNotes(ids);
            if (result) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新笔记
     */
    @PutMapping
    public Result<String> updateNote(@RequestBody Note note) {
        try {
            boolean result = noteService.updateNote(note);
            if (result) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询笔记
     */
    @GetMapping("/{id}")
    public Result<Note> getNoteById(@PathVariable Long id) {
        try {
            Note note = noteService.getNoteById(id);
            if (note != null) {
                return Result.success(note);
            } else {
                return Result.error("未找到该笔记");
            }
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询所有笔记
     */
    @GetMapping
    public Result<List<Note>> getAllNotes() {
        try {
            List<Note> notes = noteService.getAllNotes();
            return Result.success(notes);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据笔记标题查询笔记
     */
    @GetMapping("/search")
    public Result<List<Note>> searchNotes(@RequestParam(required = false) String title) {
        try {
            // 处理空字符串情况，将其转换为null
            if (title != null && title.isEmpty()) {
                title = null;
            }
            
            List<Note> notes = noteService.getNotesByNoteType(title);
            return Result.success(notes);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}